package as3.component.controls
{
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import as3.component.event.ScrollEvent;
	import as3.component.event.SizeChangeEvent;
	
	public class ScrollPane extends Canvas
	{
		
		protected var _horizontalScrollBar:ScrollBar;
		public var _verticalScrollBar:ScrollBar;
		
		protected var _verticalScrollPolicy:String = ScrollBarPolicy.OFF;
		protected var _horizontalScrollPolicy:String = ScrollBarPolicy.OFF;
		
		protected var _pageScrollSize:Number = 10;
		
		protected var vPosition:Number = 0;
		protected var hPosition:Number = 0;
		
		private var vHight:Number = 0;
		private var hWidth:Number = 0;
		
		public function ScrollPane()
		{
			alertChange = true;
			super();
		}
		
		override protected function initCom():void
		{
			super.initCom();
			addEventListener(MouseEvent.MOUSE_WHEEL, $onMouseWheel);
		}
		
		private function $onMouseWheel(evt:MouseEvent):void
		{
			var newP:Number = evt.delta < 0 ? vPosition + _verticalScrollBar.pageScrollSize : vPosition - _verticalScrollBar.pageScrollSize;
			if (needVScroll()) {
				_verticalScrollBar.setScrollPosition(newP, true);
			}
		}
		
		public function set verticalScrollPolicy(value:String):void
		{
			if (_verticalScrollPolicy == value) return;
			if (value == ScrollBarPolicy.AUTO || value == ScrollBarPolicy.OFF || value == ScrollBarPolicy.ON)
			{
				_verticalScrollPolicy = value;
			}
			reFreshScrollBarLayout();
		}
		
		public function get verticalScrollPolicy():String
		{
			return _verticalScrollPolicy;
		}
		
		public function set horizontalScrollPolicy(value:String):void
		{
			if (_horizontalScrollPolicy == value) return;
			if (value == ScrollBarPolicy.AUTO || value == ScrollBarPolicy.OFF || value == ScrollBarPolicy.ON)
			{
				_horizontalScrollPolicy = value;
			}
			reFreshScrollBarLayout();
		}
		
		public function get horizontalScrollPolicy():String
		{
			return _horizontalScrollPolicy;
		}
		
		public function set pageScrollSize(value:Number):void
		{
			if (_verticalScrollBar) {
				_pageScrollSize = value;
				_verticalScrollBar.pageScrollSize = value;
			}
		}
		
		public function get pageScrollSize() : Number
		{
			if (_verticalScrollBar) return _verticalScrollBar.pageScrollSize;
			else return 1;
		}
		
		override public function set width(value:Number):void
		{
			super.width = value;
			reFreshScrollBarLayout();
		}
		
		override public function set height(value:Number):void
		{
			super.height = value;
			reFreshScrollBarLayout();
		}
		
		protected function needVScroll():Boolean
		{
			if (_verticalScrollPolicy == ScrollBarPolicy.OFF)
			{
				return false;
			}
			if (_verticalScrollPolicy == ScrollBarPolicy.ON)
			{
				return true;
			}
			return continerHeight > height;
		}
		
		protected function needHScroll():Boolean
		{
			if (_horizontalScrollPolicy == ScrollBarPolicy.OFF)
			{
				return false;
			}
			if (_horizontalScrollPolicy == ScrollBarPolicy.ON)
			{
				return true;
			}
			return continerWidth > width;
		}
		
		private function checkCreatScrollBar(dir:String):void
		{
			var num:Number;
			switch(dir)
			{
				case ScrollBarDirection.HORIZONTAL:
					if (!_horizontalScrollBar) {
						_horizontalScrollBar = new ScrollBar();
						_horizontalScrollBar.direction = ScrollBarDirection.HORIZONTAL;
						_horizontalScrollBar.width = width;
						_horizontalScrollBar.lineScrollSize = 4;
						num = (continerWidth - width) < 0 ? 0 : (continerWidth - width);
						_horizontalScrollBar.setScrollProperties(hWidth, 0, num, _pageScrollSize);
						
						_horizontalScrollBar.addEventListener(ScrollEvent.SCROLL, onScrollBarScroll);
					}
					break;
				case ScrollBarDirection.VERTICAL:
					if (!_verticalScrollBar) {
						_verticalScrollBar = new ScrollBar();
						_verticalScrollBar.direction = ScrollBarDirection.VERTICAL;
						_verticalScrollBar.height = height;
						_verticalScrollBar.lineScrollSize = 4;
						num = (continerHeight - height) < 0 ? 0 : (continerHeight - height);
						_verticalScrollBar.setScrollProperties(vHight, 0, num, _pageScrollSize);
						_verticalScrollBar.addEventListener(ScrollEvent.SCROLL, onScrollBarScroll);
					}
					break;
			}
		}
		
		private function onScrollBarScroll(evt:ScrollEvent):void
		{
			var rect:Rectangle = continerScrollRect;
			switch(evt.target) {
				case _verticalScrollBar:
					rect.y = evt.position;
					scrollPos(rect.x, rect.y);
					vPosition = rect.y;
//					continerScrollRect = rect;
					break;
				case _horizontalScrollBar:
					rect.x = evt.position;
					scrollPos(rect.x, rect.y);
					hPosition = rect.x;
//					continerScrollRect = rect;
					break;
			}
		}
		
		private function reFreshScrollBarLayout():void
		{
			var rect:Rectangle = continerScrollRect;
			if (needVScroll()) {
				checkCreatScrollBar(ScrollBarDirection.VERTICAL);
				addChild(_verticalScrollBar);
				_verticalScrollBar.x = width - _verticalScrollBar.width;
				_verticalScrollBar.height = height;
				
			} else {
				checkCreatScrollBar(ScrollBarDirection.VERTICAL);
				if (_verticalScrollBar) {
					if (contains(_verticalScrollBar)) removeChild(_verticalScrollBar);
				}
			}
			
			if (needHScroll()) {
				checkCreatScrollBar(ScrollBarDirection.HORIZONTAL);
				addChild(_horizontalScrollBar);
				_horizontalScrollBar.y = height - _horizontalScrollBar.height;
				_horizontalScrollBar.width = width;
			} else {
				checkCreatScrollBar(ScrollBarDirection.HORIZONTAL);
				if (_horizontalScrollBar) {
					if (contains(_horizontalScrollBar)) removeChild(_horizontalScrollBar);
				}
			}
			
			
			if (needVScroll()) {
				hWidth = width - _verticalScrollBar.width;
			} else {
				hWidth = width;
				vPosition = 0;
			}
			if (needHScroll()) {
				vHight = height - _verticalScrollBar.width;
			} else {
				vHight = height;
				hPosition = 0;
			}
			rect.width = hWidth;
			rect.height = vHight;
			if (_horizontalScrollBar) {
				_horizontalScrollBar.width = hWidth;
				_horizontalScrollBar.y = height - _verticalScrollBar.width;
				_horizontalScrollBar.scrollPosition = hPosition;
			}
			if (_verticalScrollBar) {
				_verticalScrollBar.height = vHight;
				_verticalScrollBar.scrollPosition = vPosition;
			}
			continerScrollRect = rect;
			dispatchEvent(new SizeChangeEvent(SizeChangeEvent.SIZE_CHANGE));
			
			continerChange();
		}
		
		override public function continerChange():void
		{
			super.continerChange();
			var num:Number;
			if (needHScroll() && _horizontalScrollBar) {
				num = (continerWidth - hWidth) < 0 ? 0 : (continerWidth - hWidth);
//				_horizontalScrollBar.scrollPosition = hPosition;
				_horizontalScrollBar.setScrollProperties(hWidth, 0, num, _pageScrollSize);
				//这个地方如果横向滚动条需要时，请参考纵滚动条的写法
			}
			if (needVScroll()) {
				if (!_verticalScrollBar) return;
				if (!contains(_verticalScrollBar)) {
					reFreshScrollBarLayout();
					return;
				}
				num = (continerHeight - vHight) < 0 ? 0 : (continerHeight - vHight);
				_verticalScrollBar.setScrollProperties(vHight, 0, num, _pageScrollSize);
				if (_verticalScrollBar.scrollPosition == num) {
					vPosition = num;
					var rect:Rectangle = continerScrollRect;
					rect.y = vPosition;
					scrollPos(rect.x, rect.y);
				}
			} else {
				if (_verticalScrollBar && contains(_verticalScrollBar)) {
					removeChild(_verticalScrollBar);
					reFreshScrollBarLayout();
				}
			}
		}
		
		public function getVerticalScrollBar():ScrollBar
		{
			return _verticalScrollBar;
		}
		
		public function getHorizontalScrollBar():ScrollBar
		{
			return _horizontalScrollBar;
		}
	}
}